package test;

/**
 * Created by gx on 2017/4/19.
 */
import java.io.File;
import net.sourceforge.tess4j.*;
import net.sourceforge.tess4j.util.LoadLibs;

public class TesseractExample {

    public static void main(String[] args) {
//        File root = new File(System.getProperty("user.dir") + "/imgs");
//        File[] files = root.listFiles();
//        for (File file : files) {
//            String result = instance.doOCR(file);
//            String fileName = file.toString().substring(file.toString().lastIndexOf("\\")+1);
//            System.out.println("图片名：" + fileName +" 识别结果："+result);
//        }

        File imageFile = new File("E:/1.jpg");
        ITesseract instance = new Tesseract();  // JNA Interface Mapping
        File tessDataFolder = LoadLibs.extractTessResources("tessdata");

        instance.setLanguage("eng");//英文库识别数字比较准确

//Set the tessdata path
        System.out.println(tessDataFolder.getAbsolutePath());
        instance.setDatapath(tessDataFolder.getAbsolutePath());

//        instance.setDatapath("./tessdata");
        String a1=System.getProperty("user.dir");
        System.out.println(a1);

        // ITesseract instance = new Tesseract1(); // JNA Direct Mapping

        try {
            String result = instance.doOCR(imageFile);
            System.out.println(result);
        } catch (TesseractException e) {
            System.err.println(e.getMessage());
        }
    }
}
